import areaData from '@/assets/data/area.json'

/**
 * 转换省市区的JSON数据为Element Plus级联选择器需要的格式
 * @returns {Array} 转换后的级联数据
 */
export function transformData() {
  return areaData.map(province => ({
    value: province.ssqid,
    label: province.ssqname,
    children: province.city ? province.city.map(city => ({
      value: city.ssqid,
      label: city.ssqname,
      children: city.area ? city.area.map(area => ({
        value: area.ssqid,
        label: area.ssqname
      })) : []
    })) : []
  }))
}

/**
 * 根据省市区ID获取对应的名称
 * @param {string} provinceId - 省份ID
 * @param {string} cityId - 城市ID
 * @param {string} districtId - 区县ID
 * @returns {Object} 包含省市区名称的对象
 */
export function getRegionNames(provinceId, cityId, districtId) {
  const province = areaData.find(p => p.ssqid === provinceId)
  if (!province) return { province: '', city: '', district: '' }
  
  const city = province.city ? province.city.find(c => c.ssqid === cityId) : null
  if (!city) return { province: province.ssqname, city: '', district: '' }
  
  const district = city.area ? city.area.find(a => a.ssqid === districtId) : null
  
  return {
    province: province.ssqname,
    city: city.ssqname,
    district: district ? district.ssqname : ''
  }
}

/**
 * 根据省市区名称获取对应的ID
 * @param {string} provinceName - 省份名称
 * @param {string} cityName - 城市名称
 * @param {string} districtName - 区县名称
 * @returns {Object} 包含省市区ID的对象
 */
export function getRegionIds(provinceName, cityName, districtName) {
  const province = areaData.find(p => p.ssqname === provinceName)
  if (!province) return { provinceId: '', cityId: '', districtId: '' }
  
  const city = province.city ? province.city.find(c => c.ssqname === cityName) : null
  if (!city) return { provinceId: province.ssqid, cityId: '', districtId: '' }
  
  const district = city.area ? city.area.find(a => a.ssqname === districtName) : null
  
  return {
    provinceId: province.ssqid,
    cityId: city.ssqid,
    districtId: district ? district.ssqid : ''
  }
}

/**
 * 获取所有省份列表
 * @returns {Array} 省份列表
 */
export function getProvinces() {
  return areaData.map(province => ({
    value: province.ssqid,
    label: province.ssqname
  }))
}

/**
 * 根据省份ID获取城市列表
 * @param {string} provinceId - 省份ID
 * @returns {Array} 城市列表
 */
export function getCities(provinceId) {
  const province = areaData.find(p => p.ssqid === provinceId)
  if (!province || !province.city) return []
  
  return province.city.map(city => ({
    value: city.ssqid,
    label: city.ssqname
  }))
}

/**
 * 根据城市ID获取区县列表
 * @param {string} cityId - 城市ID
 * @returns {Array} 区县列表
 */
export function getDistricts(cityId) {
  for (const province of areaData) {
    if (province.city) {
      const city = province.city.find(c => c.ssqid === cityId)
      if (city && city.area) {
        return city.area.map(area => ({
          value: area.ssqid,
          label: area.ssqname
        }))
      }
    }
  }
  return []
}

/**
 * 验证省市区ID是否有效
 * @param {string} provinceId - 省份ID
 * @param {string} cityId - 城市ID
 * @param {string} districtId - 区县ID
 * @returns {boolean} 是否有效
 */
export function validateRegionIds(provinceId, cityId, districtId) {
  if (!provinceId) return false
  
  const province = areaData.find(p => p.ssqid === provinceId)
  if (!province) return false
  
  if (cityId) {
    const city = province.city ? province.city.find(c => c.ssqid === cityId) : null
    if (!city) return false
    
    if (districtId) {
      const district = city.area ? city.area.find(a => a.ssqid === districtId) : null
      if (!district) return false
    }
  }
  
  return true
}

export default {
  transformData,
  getRegionNames,
  getRegionIds,
  getProvinces,
  getCities,
  getDistricts,
  validateRegionIds
} 